package main

import (
	"bilibiliDownload/apiservice"
	"fmt"
	"net"
	"net/http"
	"os"
	"os/exec"
	"path/filepath"
	"strings"

	"github.com/zserge/lorca"
)

func main() {
	ui, _ := lorca.New("", "", 1600, 900)
	defer ui.Close()
	ln, e := net.Listen("tcp", "127.0.0.1:0")
	if e != nil {
		fmt.Println("ui服务启动失败")
		panic(e)
	}
	defer ln.Close()
	//启动静态文件服务器
	go http.Serve(ln, http.FileServer(http.Dir(getAppPath()+"/dist")))
	//启动api服务器
	go apiservice.Start()
	//加载ui界面
	if e := ui.Load(fmt.Sprintf("http://%s", ln.Addr())); e != nil {
		fmt.Println("ui加载")
		panic(e)
	}
	<-ui.Done()
}

// getAppPath 获取运行目录
func getAppPath() string {
	file, _ := exec.LookPath(os.Args[0])
	path, _ := filepath.Abs(file)
	index := strings.LastIndex(path, string(os.PathSeparator))
	return path[:index]
}
